package is.ru.honn.rumblr.data;

import is.ru.honn.rumblr.domain.AbstractRumblrPost;
import is.ru.honn.rumblr.factory.PostFactory;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: Notandi
 * Date: 11.10.2011
 * Time: 19:44
 * To change this template use File | Settings | File Templates.
 */
public class PostRowMapper implements ParameterizedRowMapper<AbstractRumblrPost>
{
    //Uses factory function to findout to create the right type of post you need, then
    //fills the values and returns the post.
    public AbstractRumblrPost mapRow(ResultSet rs, int rowNum) throws SQLException
    {
       AbstractRumblrPost post = null;
       try
       {
           post = (AbstractRumblrPost)PostFactory.createPostFromType(rs.getString(4));
           post.setId(rs.getString(1));
           post.setUrl(rs.getString(2));
           post.setDateGmt(rs.getDate(3));

       }
       catch (Exception ex)
       {
            String msg = "Unable to create post from ResultSet";
           throw new SQLException(msg);
       }
       return (AbstractRumblrPost)post;
    }

}
